<template>
  <div px-36rpx py-20px bg-white>
    <div text-28rpx mb-30rpx font-semibold>实名认证（请上传真实的个人信息，认证通过后将无法修改）</div>
    <nut-config-provider :theme-vars="{ cellBoxShadow: 'none', cellPadding: '23rpx 0', formItemTipFontSize: '24rpx' }">
      <nut-form label-position="left" :model-value="verify" :rules="rules" ref="ruleForm">
        <nut-form-item label="姓名" prop="name" :show-error-line="false">
          <nut-input v-model="verify.name" placeholder="请输入您的真实姓名" type="text" />
        </nut-form-item>
        <nut-form-item label="身份证" prop="idNumber" :show-error-line="false">
          <nut-input v-model="verify.idNumber" placeholder="请输入您的身份证号码身份证" type="text" />
        </nut-form-item>
        <nut-form-item prop="">
          <nut-checkbox v-model="isReadAndAgree" icon-size="12">
            <span link text-28rpx text-dark-color>
              已阅读并同意《
              <span link @click="$navigateTo('/pages/p?type=DISTRIBUTION_AGREEMENT')">分销推广协议</span>
              》
            </span>
          </nut-checkbox>
        </nut-form-item>
        <div text-neutral-400 text-24rpx mt-50rpx>
          您的申请信息将提交至平台审核，审核期间将会给您致电，请保持手机畅通。
        </div>
        <div text-neutral-400 text-24rpx mt-10rpx>提交认证申请后，工作人员将在三个工作日进行核对完成审核</div>
      </nut-form>
    </nut-config-provider>
    <submit title="提交" @click="submitVerify" />
  </div>
</template>

<script setup lang="ts">
import { applyDistribution } from '@/api/member'
import submit from '@/components/sheet-btn/index.vue'
import { IDCard } from '@/utils/RegExp'
import { onLoad, onShow } from '@dcloudio/uni-app'
import { getGoodsDistribution } from '@/api/goods'
import { isLogin, forceLogin } from '@/utils/filters'
import { parseScene } from '@/api/common';

interface Verify {
  name: string
  idNumber: string
}
const ruleForm = ref<any>(null)
// 认证
const verify = ref<Verify>({
  name: '',
  idNumber: ''
})
// 规则
const rules = {
  name: [
    {
      required: true,
      message: '姓名不能为空',
      trigger: 'change'
    }
  ],
  idNumber: [
    {
      required: true,
      message: '身份证号不能为空',
      trigger: 'change'
    },
    {
      message: '请填写正确的身份证号',
      validator: (val: string) => {
        return IDCard.test(val)
      },
      trigger: ['change', 'blur']
    }
  ]
}
const isReadAndAgree = ref<Boolean>(false)
const routerVal = ref<any>('')
const distributionId = ref<any>('') // 分销商id

function submitVerify() {
  ruleForm.value.validate().then(async ({ valid }: any) => {
    if (valid) {
      if (!isReadAndAgree.value) {
        uni.showToast({ title: '请先阅读并同意分销推广协议', duration: 2000, icon: 'none' })
      } else {
        const res = await applyDistribution(verify.value)
        if (res.data.success) {
          uni.showToast({ title: '提交成功', icon: 'none' })
          setTimeout(() => {
            uni.switchTab({ url: '/pages/tabbar/shopMine' })
          }, 500)
        }
      }
    } else {
      uni.showToast({ title: '请填写完整信息', icon: 'none' })
    }
  })
}

// 绑定分销员
async function init(distributionId:any) {
  await getGoodsDistribution(distributionId)
}

onLoad(async(options: any) => {
  if (options.scene) {
    // 获取真实scene参数
    const scene = await parseScene(decodeURIComponent(options.scene))
    options.distributionId = scene
  }
  routerVal.value = options
  console.log(options)
})
onShow(async () => {
  // 判断当前用户是否登录
  forceLogin()
  if (isLogin('auth')) {
    if (routerVal.value.distributionId) {
      distributionId.value = routerVal.value.distributionId
      init(distributionId.value)
    }
  }
})
</script>

<style scoped lang="scss"></style>
